package linkList;

public class N19_RemoveNthFromEnd {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode virtualHead = new ListNode(0, head);
        ListNode slow = virtualHead, fast = virtualHead;
        for (int i = 0; i < n + 1; i++)
            fast = fast.next;

        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return virtualHead.next;
    }

    public static void main(String[] args) {
        N19_RemoveNthFromEnd solution = new N19_RemoveNthFromEnd();
        int[] arr = {1, 2, 3, 4};
        ListNode head = ListNode.generaListByArray(arr);
        head = solution.removeNthFromEnd(head, 2);
        ListNode.printList(head);
    }
}
